package com.zhaoc.service.impl;

import com.zhaoc.dao.WayBillRepository;
import com.zhaoc.domain.take_delivery.WayBill;
import com.zhaoc.index.WayBillIndexRepository;
import com.zhaoc.service.IWaybillService;
import org.apache.commons.lang3.StringUtils;
import org.elasticsearch.index.query.*;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.elasticsearch.core.query.NativeSearchQuery;
import org.springframework.data.elasticsearch.core.query.SearchQuery;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * <p>title:com.zhaoc.service.impl</p>
 * date:2018/8/16 0016
 * time:20:08
 * author:赵成
 * user:13566
 */
@Service
@Transactional
public class WayBillServiceImpl implements IWaybillService {

    @Autowired
    private WayBillRepository wayBillRepository;

    @Autowired
    private WayBillIndexRepository wayBillIndexRepository;

    //保存运单
    @Override
    public void save(WayBill wayBill) {
        //判断运单号是否存在
        WayBill persistWayBillNum = wayBillRepository.findByWayBillNum(wayBill.getWayBillNum());
        if (persistWayBillNum == null || persistWayBillNum.getId() == null) {
            //运单不存在
            wayBillRepository.save(wayBill);
            //保存索引
            wayBillIndexRepository.save(wayBill);
        } else {
            try {
                Integer id = persistWayBillNum.getId();
                BeanUtils.copyProperties(persistWayBillNum, wayBill);
                persistWayBillNum.setId(id);
                //保存索引
                wayBillIndexRepository.save(wayBill);
            } catch (Exception e) {
                e.printStackTrace();
                throw new RuntimeException(e.getMessage());
            }

        }
    }

    //分页查询
    @Override
    public Page<WayBill> findPageData(WayBill wayBill, Pageable pageable) {
        //先判断是否有条件存在
        if (StringUtils.isBlank(wayBill.getWayBillNum()) && StringUtils.isBlank(wayBill.getSendAddress()) &&
                StringUtils.isBlank(wayBill.getRecAddress()) && StringUtils.isBlank(wayBill.getSendProNum()) &&
                (wayBill.getSignStatus() == null || wayBill.getSignStatus() == 0)) {
            //当没有条件的时候直接调用dao层查询
            return wayBillRepository.findAll(pageable);
        } else {
            /**
             * 查询条件
             * must 条件必须成立 类似 and
             * must not 条件必须不成立 类似not
             * should 条件可以成立 类似于or
             */
            BoolQueryBuilder query = new BoolQueryBuilder();//布尔查询,多条件查询
            if (StringUtils.isNotBlank(wayBill.getWayBillNum())) {
                //运单号查询---精准查询
                QueryBuilder tempQuery = new TermQueryBuilder("wayBillNum", wayBill.getWayBillNum());
                query.must(tempQuery);
            }
            if (StringUtils.isNotBlank(wayBill.getSendAddress())) {
                //发货地点查询---模糊查询
                //情况一:输入"北",是查询词条的一部分,使用模糊匹配词条查询
                QueryBuilder wildCardQuery = new WildcardQueryBuilder("sendAddress", "*" + wayBill.getSendAddress() +
                        "*");
                //情况二:输入"北京市海淀区" 是多个词条的组合,进行分词后 每个词条匹配查询
                QueryBuilder queryStringQueryBuilder = new QueryStringQueryBuilder(wayBill.getSendAddress()).field
                        ("sendAddress").defaultOperator(QueryStringQueryBuilder.Operator.AND);
                //两种取或的关系
                BoolQueryBuilder boolQueryBuilder = new BoolQueryBuilder();
                boolQueryBuilder.should(wildCardQuery);
                boolQueryBuilder.should(queryStringQueryBuilder);

                query.must(boolQueryBuilder);

            }
            if (StringUtils.isNotBlank(wayBill.getRecAddress())){
                //收货地点查询---模糊查询
                //情况一:输入"北",是查询词条的一部分,使用模糊匹配词条查询
                QueryBuilder wildCardQuery = new WildcardQueryBuilder("recAddress", "*" + wayBill.getRecAddress() +
                        "*");
                //情况二:输入"北京市海淀区" 是多个词条的组合,进行分词后 每个词条匹配查询
                QueryBuilder queryStringQueryBuilder = new QueryStringQueryBuilder(wayBill.getRecAddress()).field
                        ("recAddress").defaultOperator(QueryStringQueryBuilder.Operator.AND);
                //两种取或的关系
                BoolQueryBuilder boolQueryBuilder = new BoolQueryBuilder();
                boolQueryBuilder.should(wildCardQuery);
                boolQueryBuilder.should(queryStringQueryBuilder);

                query.must(boolQueryBuilder);
            }
            if (StringUtils.isNotBlank(wayBill.getSendProNum())){
                //根据速运类型查询---精准查询
                QueryBuilder termQuery = new TermQueryBuilder("sendProNum",wayBill.getSendProNum());
                query.must(termQuery);
            }
            if (wayBill.getSignStatus() != null && wayBill.getSignStatus() != 0){
                //根据签收状态查询---精准查询
                QueryBuilder termQuery = new TermQueryBuilder("signStatus",wayBill.getSignStatus());
                query.must(termQuery);
            }


            SearchQuery searchQuery = new NativeSearchQuery(query);
            searchQuery.setPageable(pageable);//添加分页效果
            //有条件查询,查询索引库
            Page<WayBill> search = wayBillIndexRepository.search(searchQuery);
            return search;
        }

    }

    //根据运单号查询运单信息
    @Override
    public WayBill findByWayBillNum(String wayBillNum) {
        return wayBillRepository.findByWayBillNum(wayBillNum);
    }
}
